//是否是数组
function isArray(obj) {
  return Array.isArray(obj);
}
function isNumber(obj) {
  return toRawType(obj) === "Number";
}
//伪转数组
function toArray(list, start) {
  list = isUnder(list) ? [] : list;
  return Array.prototype.slice.call(list, start);
}
function isUnder(v) {
  return v === undefined || v === null;
}
function toRawType(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1);
}
function isPlainObject(obj) {
  return toRawType(obj) === "Object";
}

//合并对象,数组
function _extend(a, b, key) {
  if (isArray(a) && isArray(b)) {
    //特殊处理
    if (key === "colorStops" || key === "radius") {
      return b;
    }
    return [...a, ...b];
  } else if (isPlainObject(a) && isPlainObject(b)) {
    let obj = { ...a };
    for (let k in b) {
      obj[k] = _extend(obj[k], b[k], k);
    }
    return obj;
  } else {
    return b;
  }
}

function extend() {
  let args = toArray(arguments);
  if (args.length <= 1) {
    return args[0] ? args[0] : {};
  }
  let obj = {};
  args.forEach((v) => {
    if (!isPlainObject(v)) {
      return;
    }
    obj = _extend(obj, v);
  });
  return obj;
}

//是否是函数
function isFunction(obj) {
  return typeof obj === "function";
}

//防抖
function debounce(fn, timeout = 500) {
  let timer;
  return function () {
    timer && clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, arguments);
    }, timeout);
  };
}
//万,亿单位转换
function num2Unit(val, point = 2) {
  if (!isNumber(val)) {
    return 0;
  }
  //单位,数字
  let unit = "",
    num = val;
  //获取整数部分
  let numInt = Math.floor(val).toString(),
    numIntLen = numInt.length - 1; //减一位
  //获取亿
  let yiLen = Math.floor(numIntLen / 8);
  for (let i = 0; i < yiLen; i++) {
    unit += "亿";
    num = num / Math.pow(10, 8);
  }
  //获取万
  let wanLen = Math.floor((numIntLen % 8) / 4);
  for (let i = 0; i < wanLen; i++) {
    unit = "万" + unit;
    num = num / Math.pow(10, 4);
  }
  let pointVal = Math.pow(10, point);
  let numToStr = num === 0 ? "0" : Math.round(num * pointVal) / pointVal;
  return numToStr + unit;
}
export default {
  extend,
  isArray,
  isPlainObject,
  isFunction,
  debounce,
  num2Unit,
};
export { extend, isArray, isPlainObject, isFunction, debounce, num2Unit };
